package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.bean.SunriseSunsetTimeBean;
import com.lxj.xpopup.core.BottomPopupView;

import java.util.ArrayList;
import java.util.List;

public class BottomSunriseSunsetDialog extends BottomPopupView {

    private DialogClickListener mDialogClickListener;
    private TextView tvLabel;
    private TextView tvSelected;
    private WheelView wv_first;
    private ArrayWheelAdapter mFirstArrayWheelAdapter;
    //日出 : 1 ,日落 : 2
    private int type = 1;
    private int timeLong;

    public BottomSunriseSunsetDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_sunrise_sunset;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        tvLabel = findViewById(R.id.tv_label);
        tvSelected = findViewById(R.id.tv_selected);
        wv_first = findViewById(R.id.options1);
        View btnSave = findViewById(R.id.btn_save);
        btnSave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mDialogClickListener != null){
                    SunriseSunsetTimeBean sunriseSunsetTimeBean = (SunriseSunsetTimeBean) mFirstArrayWheelAdapter.getItem(wv_first.getCurrentItem());
                    mDialogClickListener.onConfirmClick(sunriseSunsetTimeBean);
                }
            }
        });
        setStyle();
        initWheelView();
    }

    private void setStyle() {
        wv_first.setGravity(Gravity.CENTER);
        wv_first.setCyclic(false);
        wv_first.setAlphaGradient(true);
//        wv_first.setLabelOffset(5);
        wv_first.setItemsVisibleCount(5);
        wv_first.setDividerColor(0x00000000);
        wv_first.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_first.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));
        wv_first.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                SunriseSunsetTimeBean sunriseSunsetTimeBean = (SunriseSunsetTimeBean) mFirstArrayWheelAdapter.getItem(index);
                tvSelected.setText(sunriseSunsetTimeBean.getPickerViewText());
            }
        });
    }

    private void initWheelView(){
        List<SunriseSunsetTimeBean> items = new ArrayList<>();
        int currentItem = 5;
        for(int i=0; i<11; i++){
            SunriseSunsetTimeBean sunriseSunsetTimeBean = new SunriseSunsetTimeBean();
            switch (i){
                case 0:
                    sunriseSunsetTimeBean.setLabel(getResources().getString(R.string.when_2_hours_ago));
                    sunriseSunsetTimeBean.setValue(-120 * 60);
                    break;
                case 1:
                    sunriseSunsetTimeBean.setLabel(getResources().getString(R.string.when_1_hours_ago));
                    sunriseSunsetTimeBean.setValue(-60 * 60);
                    break;
                case 2:
                    sunriseSunsetTimeBean.setLabel(getResources().getString(R.string.when_30_minutes_ago));
                    sunriseSunsetTimeBean.setValue(-30 * 60);
                    break;
                case 3:
                    sunriseSunsetTimeBean.setLabel(getResources().getString(R.string.when_10_minutes_ago));
                    sunriseSunsetTimeBean.setValue(-10 * 60);
                    break;
                case 4:
                    sunriseSunsetTimeBean.setLabel(getResources().getString(R.string.when_5_minutes_ago));
                    sunriseSunsetTimeBean.setValue(-5 * 60);
                    break;
                case 5:
                    sunriseSunsetTimeBean.setLabel(getResources().getString(R.string.when_time));
                    sunriseSunsetTimeBean.setValue(0);
                    break;
                case 6:
                    sunriseSunsetTimeBean.setLabel(getResources().getString(R.string.when_5_minutes_later));
                    sunriseSunsetTimeBean.setValue(5 * 60);
                    break;
                case 7:
                    sunriseSunsetTimeBean.setLabel(getResources().getString(R.string.when_10_minutes_later));
                    sunriseSunsetTimeBean.setValue(10 * 60);
                    break;
                case 8:
                    sunriseSunsetTimeBean.setLabel(getResources().getString(R.string.when_30_minutes_later));
                    sunriseSunsetTimeBean.setValue(30 * 60);
                    break;
                case 9:
                    sunriseSunsetTimeBean.setLabel(getResources().getString(R.string.when_1_hours_later));
                    sunriseSunsetTimeBean.setValue(60 * 60);
                    break;
                case 10:
                    sunriseSunsetTimeBean.setLabel(getResources().getString(R.string.when_2_hours_later));
                    sunriseSunsetTimeBean.setValue(120 * 60);
                    break;
            }
            if(sunriseSunsetTimeBean.getValue() == timeLong){
                currentItem = i;
            }
            items.add(sunriseSunsetTimeBean);
        }
        mFirstArrayWheelAdapter = new ArrayWheelAdapter(items){
            @Override
            public Object getItem(int index) {
                SunriseSunsetTimeBean sunriseSunsetTimeBean = (SunriseSunsetTimeBean) super.getItem(index);
                switch (type) {
                    case 1:
                        sunriseSunsetTimeBean.setSunriseSunsetType(getResources().getString(R.string.sunrise));
                        break;
                    case 2:
                        sunriseSunsetTimeBean.setSunriseSunsetType(getResources().getString(R.string.sunset));
                        break;
                }
                return sunriseSunsetTimeBean;
            }
        };
        wv_first.setAdapter(mFirstArrayWheelAdapter);
        wv_first.setCurrentItem(currentItem);
    }

    public void setSunriseSunsetType(int type){
        this.type = type;
    }

    public void setTimeLong(int timeLong){
        this.timeLong = timeLong;
        if(wv_first != null){
            for(int i=0; i<mFirstArrayWheelAdapter.getItemsCount(); i++){
                SunriseSunsetTimeBean sunriseSunsetTimeBean = (SunriseSunsetTimeBean) mFirstArrayWheelAdapter.getItem(i);
                if(sunriseSunsetTimeBean.getValue() == timeLong){
                    wv_first.setCurrentItem(i);
                    tvSelected.setText(sunriseSunsetTimeBean.getPickerViewText());
                    break;
                }
            }
        }
    }

    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    public void setDialogClickListener(@Nullable DialogClickListener listener) {
        mDialogClickListener = listener;
    }

    public interface DialogClickListener {
        void onConfirmClick(SunriseSunsetTimeBean sunriseSunsetTimeBean);
    }
}
